package com.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * Date: 2024/7/2 21:08
 * Author: T_log
 * Description: CuratorLockDemo
 */
public class CuratorLockDemo {

    public static void main(String[] args) throws Exception {
        ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", 5000, 3000, retryPolicy);

        client.start();

        System.out.println("已经与zookeeper建立连接了.....");

        InterProcessReadWriteLock readWriteLock = new InterProcessReadWriteLock(client, "/locks/lock");

        InterProcessMutex writeLock = readWriteLock.writeLock();
        writeLock.acquire();
        writeLock.release();


        InterProcessMutex readLock = readWriteLock.readLock();
        readLock.acquire();
        readLock.release();

        Thread.sleep(Integer.MAX_VALUE);
    }
}
